#include "stdafx.h"
#include "serialise/json.h"
#include "serialise/jsonserialise.h"

namespace Serialisation
{

std::map<std::string, Ut::SharedPtr<Handler<Json::Serialiser>>> HandlerMap<Json::Serialiser>::s_handlerMap;

template HandlerMap<Json::Serialiser>;

}

///////////////////////////////////////////////////////////////

namespace Json
{

Deserialiser::Deserialiser(std::ifstream& file)
{
	m_pData = Ut::MakeShared(Json::Parse(file));

	if (m_pData)
	{
		m_objects.push_back(m_pData);
	}
}

Deserialiser::Deserialiser(Ut::SharedPtr<Json::JObject> pObject)
{
	m_pData = pObject;

	if (m_pData)
	{
		m_objects.push_back(m_pData);
	}
}

bool Deserialiser::BeginObject(const char* name)
{
	if (m_objects.empty())
		return false;

	Json::JObject* pObject = m_objects.back()->GetObject(name);

	if (pObject)
	{
		m_objects.push_back(pObject);
		return true;
	}

	return false;
}

bool Deserialiser::BeginObject(int i)
{
	if (m_arrays.empty())
		return false;

	Json::JObject* pObject = m_arrays.back()->GetObject(i);

	if (pObject)
	{
		m_objects.push_back(pObject);
		return true;
	}

	return false;
}

int Deserialiser::ArraySize()
{
	return m_arrays.back()->Size();
}

void Deserialiser::EndObject()
{
	m_objects.pop_back();
}

bool Deserialiser::BeginArray(const char* name)
{
	if (m_objects.empty())
		return false;

	Json::JArray* pArray = m_objects.back()->GetArray(name);

	if (pArray)
	{
		m_arrays.push_back(pArray);
		return true;
	}

	return false;
}

bool Deserialiser::BeginArray(int i)
{
	if (m_arrays.empty())
		return false;

	Json::JArray* pArray = m_arrays.back()->GetArray(i);

	if (pArray)
	{
		m_arrays.push_back(pArray);
		return true;
	}

	return false;
}

void Deserialiser::EndArray()
{
	m_arrays.pop_back();
}

void Deserialiser::Read(const char* name, int& v) {m_objects.back()->GetData(name, v);}
void Deserialiser::Read(const char* name, bool& v) {m_objects.back()->GetData(name, v);}
void Deserialiser::Read(const char* name, float& v) {m_objects.back()->GetData(name, v);}
void Deserialiser::Read(const char* name, uint8& v) {int temp; m_objects.back()->GetData(name, temp); v = (uint8) temp;}
void Deserialiser::Read(const char* name, uint16& v) {m_objects.back()->GetData(name, v);}
void Deserialiser::Read(const char* name, uint32& v) {m_objects.back()->GetData(name, v);}
void Deserialiser::Read(const char* name, uint64& v) {m_objects.back()->GetData(name, v);}
void Deserialiser::Read(const char* name, int8& v) {m_objects.back()->GetData(name, v);}
void Deserialiser::Read(const char* name, int16& v) {m_objects.back()->GetData(name, v);}
void Deserialiser::Read(const char* name, int64& v) {m_objects.back()->GetData(name, v);}
void Deserialiser::Read(const char* name, std::string& v) {m_objects.back()->GetData(name, v);}

void Deserialiser::Read(int i, int& v) {m_arrays.back()->GetData(i, v);}
void Deserialiser::Read(int i, bool& v) {m_arrays.back()->GetData(i, v);}
void Deserialiser::Read(int i, float& v) {m_arrays.back()->GetData(i, v);}
void Deserialiser::Read(int i, uint8& v) {int temp; m_arrays.back()->GetData(i, temp); v = (uint8) temp;}
void Deserialiser::Read(int i, uint16& v) {m_arrays.back()->GetData(i, v);}
void Deserialiser::Read(int i, uint32& v) {m_arrays.back()->GetData(i, v);}
void Deserialiser::Read(int i, uint64& v) {m_arrays.back()->GetData(i, v);}
void Deserialiser::Read(int i, int8& v) {m_arrays.back()->GetData(i, v);}
void Deserialiser::Read(int i, int16& v) {m_arrays.back()->GetData(i, v);}
void Deserialiser::Read(int i, int64& v) {m_arrays.back()->GetData(i, v);}
void Deserialiser::Read(int i, std::string& v) {m_arrays.back()->GetData(i, v);}

}